package com.base.springboot.starter.mybatisplus.domain;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.base.common.model.po.UserInfo;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.FieldNameConstants;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * MybatisPlus entity 封装类
 *
 * @author admin
 * @date 2020/5/19 AM10:15
 */
@Data
@FieldNameConstants
public class GenericEntity extends Model<GenericEntity> implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 数据库 主键
     */
    @ApiModelProperty(value = "主键id")
    protected Long id;

    @ApiModelProperty(value = "创建者")
    private String insertUserId;

    @ApiModelProperty(value = "创建者name")
    private String insertUserName;

    @ApiModelProperty(value = "创建时间")
    @TableField(fill = FieldFill.INSERT)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime insertDate;

    @ApiModelProperty(value = "更新人")
    private String updateUserId;

    @ApiModelProperty(value = "更新人name")
    private String updateUserName;

    @ApiModelProperty(value = "更新时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime updateDate;

    @ApiModelProperty(value = "所属系统")
    private String btSystem;

    @ApiModelProperty(value = "所属用户")
    private String btUser;

    @ApiModelProperty(value = "删除标记")
    @TableLogic
    private Integer delFlag;

    @ApiModelProperty(value = "备注")
    private String remarks;

    /**
     * 新增公共属性
     *
     * @param userInfo 用户信息
     */
    public void addUser(UserInfo userInfo) {
        this.setInsertUserId(userInfo.getId().toString());
        this.setInsertUserName(userInfo.getName());
        this.setBtSystem(userInfo.getBtSystem());
        this.setBtUser(userInfo.getBelongId());
        this.setUpdateUserId(userInfo.getId().toString());
        this.setUpdateUserName(userInfo.getName());
    }

    /**
     * 更新公共属性
     *
     * @param userInfo  用户信息
     */
    public void updateUser(UserInfo userInfo) {
        this.setBtSystem(userInfo.getBtSystem());
        this.setBtUser(userInfo.getBtUser());
        this.setUpdateUserId(userInfo.getId().toString());
        this.setUpdateUserName(userInfo.getName());
    }
}
